package client;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Login extends JFrame {
	
	private JTextField serverName;
	private JTextField serverPort;
	private JTextField username;
	private JTextField password;
	private ServerConnection conn;
	
	public Login(ServerConnection conn) {
		super("Login");
		this.conn = conn;
		this.setLocation(1300, 400);
		JPanel pane = new JPanel();
		pane.setLayout(new BoxLayout(pane, BoxLayout.PAGE_AXIS));
		
		serverName = new JTextField("localhost");
		serverPort = new JTextField("4343");
		username = new JTextField("test");
		password = new JTextField("pass");
		
		pane.add(new JLabel("Server:"));
		pane.add(serverName);
		pane.add(new JLabel("Port:"));
		pane.add(serverPort);
		pane.add(new JLabel("Name:"));
		pane.add(username);
		pane.add(new JLabel("Password:"));
		pane.add(password);
		pane.add(new LoginButton());
		pane.add(new CloseButton());
		
		setContentPane(pane);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		pack();
		setVisible(true);
	}
	
	private class LoginButton extends JButton implements ActionListener {
		
		public LoginButton() {
			super("Login");
			addActionListener(this);
		}

		@Override
		public void actionPerformed(ActionEvent arg0) {
			conn.setServer(serverName.getText(), Integer.parseInt(serverPort.getText()));
			conn.login(username.getText(), password.getText());
		}	
	}
	private class CloseButton extends JButton implements ActionListener {
		public CloseButton() {
			super("close");
			addActionListener(this);
		}
		public void actionPerformed(ActionEvent e) {
			conn.close();
		}
	}

}
